package com.fosu.web.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.fosu.common.ResponseResult;
import com.fosu.domain.Product;
import com.fosu.service.ProductService;
import com.fosu.service.impl.ProductServiceImpl;
import com.fosu.vo.MiniProduct;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * 商品类接口
 * @author manno
 * @date 2020/10/13
 */
@RestController
@CrossOrigin
@RequestMapping("product")
public class ProductController {
    @Autowired
    ProductService productService;

    /**根据商品id查找商品
     * @param productId 商品id
     * @return Product 商品实体类对象
     * @throws Exception*/
    @GetMapping("/getProductById")
    public ResponseResult getProductById(@RequestParam("productId") int productId, HttpServletResponse resp) throws Exception {
        return productService.findProductById(resp, productId);
    }

    /**获得最新的20条商品信息
     * @param resp Servlet响应对象
     * @param currpage 当前页数
     * @param pagesize 页面大小
     * @return ResponseResult 响应结果
     * @throws Exception*/
    @GetMapping("/getProducts")
    public ResponseResult getProducts(@RequestParam int currpage
            , @RequestParam int pagesize, HttpServletResponse resp) throws Exception {
        return productService.getProducts(resp, currpage, pagesize);
    }

    /**根据所选校区和分类查找商品
     * @param resp Servlet响应对象
     * @return ResponseResult 响应结果
     * @throws Exception*/
    @PostMapping("/getSpecificProducts")
    public ResponseResult getSpecificProducts(@RequestBody JSONObject jsonObject
            , HttpServletResponse resp
    ) throws Exception {
        return productService.getProductsByCampusAndCategories(resp,jsonObject);
    }


    /**根据字串查询
     * @param resp Servlet响应对象
     * @param jsonObject 搜索信息
     * @return ResponseResult 响应结果
     * @throws Exception
     */
    @RequestMapping(value = "/search")
    public ResponseResult search(@RequestBody JSONObject jsonObject, HttpServletResponse resp) throws Exception {
        return productService.findMsgBySearch(resp, jsonObject);
    }

    /**根据输入字串查询更多
     * @param resp Servlet响应对象
     * @param jsonObject 搜索信息
     * @return ResponseResult 响应结果
     * @throws Exception
     */
    @RequestMapping(value = "/searchMoreAboutMsg")
    public ResponseResult searchMoreAboutMsg(@RequestBody JSONObject jsonObject, HttpServletResponse resp) throws Exception {
        return productService.findMoreAboutMsg(resp, jsonObject);
    }

    /**根据自定义分类查询更多
     * @param resp Servlet响应对象
     * @param jsonObject 搜索信息
     * @return ResponseResult 响应结果
     * @throws Exception
     */
    @RequestMapping(value = "/searchMoreAboutCustomizeCategories")
    public ResponseResult searchMoreAboutCustomizeCategories(@RequestBody JSONObject jsonObject, HttpServletResponse resp) throws Exception {
        return productService.findMoreAboutCustomizeCategories(resp, jsonObject);
    }

    /**根据校区查询更多
     * @param resp Servlet响应对象
     * @param jsonObject 搜索信息
     * @return ResponseResult 响应结果
     * @throws Exception
     */
    @RequestMapping(value = "/searchMoreAboutCampus")
    public ResponseResult searchMoreAboutCampus(@RequestBody JSONObject jsonObject, HttpServletResponse resp) throws Exception {
        return productService.findMoreAboutCampus(resp, jsonObject);
    }

    /**根据分类查询更多
     * @param resp Servlet响应对象
     * @param jsonObject 搜索信息
     * @return ResponseResult 响应结果
     * @throws Exception
     */
    @RequestMapping(value = "/searchMoreAboutCategories")
    public ResponseResult searchMoreAboutCategories(@RequestBody JSONObject jsonObject, HttpServletResponse resp) throws Exception {
        return productService.findMoreAboutCategories(resp, jsonObject);
    }


    /**切换商品收藏状态
     * @param resp Servlet响应对象
     * @param userId 用户id
     * @param productId 商品id
     * @return ResponseResult 响应结果
     * */
    @RequestMapping(value = "/switchProductFavorite")
    public ResponseResult switchFavorite(@RequestParam("userId") Integer userId,@RequestParam("productId") Integer productId, HttpServletResponse resp)
    {
        return productService.switchWhetherToFavorite(resp, userId, productId);
    }

    /**查看收藏状态
     * @param resp Servlet响应对象
     * @param userId 用户id
     * @param productId 商品id
     * @return ResponseResult 响应结果
     * */
    @RequestMapping(value = "/getProductFavorite")
    public ResponseResult getProductFavorite(@RequestParam Integer userId, @RequestParam Integer productId, HttpServletResponse resp) {
        return productService.findIsFavorite(resp, userId, productId);
    }

    /**获得用户发布的商品集合
     * @param resp Servlet响应对象
     * @param userId 用户id
     * @param currPage 当前页数
     * @param pageSize 页大小
     * @return ResponseResult 响应结果
     */
    @GetMapping("/userReleasedProducts")
    public ResponseResult userReleased(HttpServletResponse resp
            , @RequestParam("userId") Integer userId
            ,@RequestParam("currPage") int currPage
            , @RequestParam("pageSize") int pageSize)
    {
        return productService.userReleased(resp,userId,currPage,pageSize);
    }

    /**查看商品今日内是否已被该用户浏览
     * 如果未被该用户浏览过就把浏览量+1
     * @param userId 当前正在浏览的用户id
     * @param productId 被浏览的商品id
     * @return ResponseResult 响应结果
     */
    @GetMapping("addPageviews")
    ResponseResult addViewed(HttpServletResponse resp, Integer userId, Integer productId)
    {
        return productService.addViewed(resp, userId, productId);
    }


    @GetMapping("/getFavoriteProductByUserId")
    public ResponseResult getFavoriteProductByUserId(HttpServletResponse resp
            , @RequestParam("userId") Integer userId
            ,@RequestParam("currPage") Integer currPage
            ,@RequestParam("pageSize") Integer pageSize) {
        return productService.getFavoriteProductByUserId(resp, userId,currPage,pageSize);
    }

    @GetMapping("/getFavoriteProductCount")
    public ResponseResult getFavoriteProductCount(HttpServletResponse resp, @RequestParam("userId") Integer userId) {
        return productService.getFavoriteProductCount(resp, userId);
    }

    @GetMapping("/offShelfProduct")
    ResponseResult offShelfProduct(HttpServletResponse resp,@RequestParam("productId") Integer productId)
    {
        return productService.offShelfProduct(resp, productId);
    }
}
